﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace esome.SubPage
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MeterView : ContentView
    {
        public MeterView()
        {
            InitializeComponent();
        }
        private double factor = 1;
        private void MeterCalc_Click(object sender, EventArgs e)
        {
            double.TryParse(txt_U.Text, out double U);
            double Un = U;
            double Unom = U;
            double.TryParse(txt_I.Text, out double I);
            double.TryParse(txt_C.Text, out double C);
            double m = 1;
            switch (cmb_TestMode.SelectedItem.ToString())
            {
                case "1P2W": m = 1; break;
                case "3P3W": m = Math.Sqrt(3); break;
                case "3P4W": m = 3; break;
            }

            bool IsMinus = false;
            if (!string.IsNullOrWhiteSpace(txt_PF.Text))//按有功功率因数输入，无功转化为有功输入
            {
                double.TryParse(txt_PF.Text, out factor);
                if (txt_PF.Text.IndexOf("-") >= 0) IsMinus = true;
                if (cmb_FR.SelectedItem.ToString() == "-") IsMinus = true;//Reverse
            }
            double s = U * I * m;
            double power = s * factor;
            double CFreq = Math.Abs(power * C / 3600 / 1000);
            double CTime = 0;
            if (CFreq != 0)
            {
                CTime = 1 / CFreq;
            }
            double seconds = CTime * C;

            double angle = Math.Acos(Math.Abs(factor)) / Math.PI * 180;
            if (IsMinus) angle = angle + 180;
            if ("Capacitive".Equals(cmb_LC.SelectedItem.ToString()))
            {
                angle = angle * -1;
                if (angle < 0) angle = angle + 360;
            }
            txt_P.Text = Math.Round(power, 5).ToString();
            txt_Q.Text = Math.Round(Math.Sqrt(s * s - power * power), 5).ToString();
            txt_S.Text = Math.Round(s, 5).ToString();
            txt_A.Text = Math.Round(angle, 5).ToString();
            txt_CHz.Text = Math.Round(CFreq, 5).ToString();
            txt_OnePulseTime.Text = Math.Round(CTime, 5).ToString("F");
            txt_TimePerkWh.Text = Math.Round(seconds, 5).ToString("F");
            txt_MinPerkWh.Text = Math.Round((seconds / 60F), 5).ToString("F");

            PowerHelper.CalculatePhaseDegree($"M{cmb_TestMode.SelectedItem.ToString()}", cmb_PQ.SelectedItem.ToString(), cmb_FR.SelectedItem.ToString() == "-" ? "Reverse" : "Forward", cmb_HABC.SelectedItem.ToString(), factor, cmb_LC.SelectedItem.ToString(), cmb_clockwise.SelectedItem.ToString().ToLower() == "anticlockwise", out PowerHelper.PhiParam phis);
            txt_PhiU1.Text = phis.PhiUa.ToString();
            txt_PhiU2.Text = phis.PhiUb.ToString();
            txt_PhiU3.Text = phis.PhiUc.ToString();
            txt_PhiI1.Text = phis.PhiIa.ToString();
            txt_PhiI2.Text = phis.PhiIb.ToString();
            txt_PhiI3.Text = phis.PhiIc.ToString();
            VectorHelper vector = new VectorHelper()
            {
                UL1Angle = (float)phis.PhiUa,
                UL2Angle = (float)phis.PhiUb,
                UL3Angle = (float)phis.PhiUc,
                IL1Angle = (float)phis.PhiIa,
                IL2Angle = (float)phis.PhiIb,
                IL3Angle = (float)phis.PhiIc,
            };
            switch (cmb_TestMode.SelectedItem.ToString())
            {
                case "1P2W":
                    vector.UL2Value = 0;
                    vector.UL3Value = 0;
                    vector.IL2Value = 0;
                    vector.IL3Value = 0;
                    break;
                case "3P3W":
                    vector.IL2Value = 0;
                    break;
                case "3P4W":
                    break;
            }
            //if (vector.DrawVector()) pic_phase.Source = PicHelper.GetBitmap(vector.VectorFullPath);
            //else pic_phase.Source = null;
            double.TryParse(txt_Co.Text, out double C0);
            double.TryParse(txt_N.Text, out double N);
            double T0 = N * CTime;
            double m0 = C0 / C * N;
            txt_m0.Text = m0.ToString();
            txt_T0.Text = T0.ToString();

            double.TryParse(txt_Imin.Text, out double Imin);
            double.TryParse(txt_Itr.Text, out double Itr);
            double.TryParse(txt_Ib.Text, out double Ib);
            double.TryParse(txt_Imax.Text, out double Imax);
            double Iqx = 0.005;
            //JJG596-2012
            double Iq;
            switch (cmb_Transformer.SelectedItem.ToString())
            {
                case "Direct":
                    //switch (cmb_MeterClass.SelectedItem.ToString())
                    //{
                    //    case "A":
                    //    case "2": Iqx = 0.005; break;
                    //    case "B":
                    //    case "1": Iqx = 0.004; break;
                    //    case "C":
                    //    case "0.5S":
                    //    case "D":
                    //    case "0.2S":
                    //    case "E": Iqx = 0.004; break;
                    //}
                    //A 2
                    Iqx = 0.005;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    //B 1
                    Iqx = 0.004;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb_1.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime_1.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    //C 0.5S
                    Iqx = 0.004;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb_05S.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime_05S.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    //D 0.2S E
                    Iqx = 0.004;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb_02S.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime_02S.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    break;
                case "Transformer":
                    //switch (cmb_MeterClass.SelectedItem.ToString())
                    //{
                    //    case "A":
                    //    case "2": Iqx = 0.003; break;
                    //    case "B":
                    //    case "1": Iqx = 0.002; break;
                    //    case "C":
                    //    case "0.5S":
                    //    case "D":
                    //    case "0.2S":
                    //    case "E": Iqx = 0.001; break;
                    //}
                    //A 2
                    Iqx = 0.003;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    //B 1
                    Iqx = 0.002;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb_1.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime_1.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    //C 0.5S
                    Iqx = 0.001;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb_05S.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime_05S.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    //D 0.2S E
                    Iqx = 0.001;
                    Iq = Iqx * Ib;
                    txt_jjg596_2012_startxIb_02S.Text = Iqx.ToString();
                    txt_jjg596_2012_startTime_02S.Text = Math.Round(1.2 * 60 * 1000 / (C * m * Un * Iq), 5).ToString();
                    break;
            }

            double b = 480;
            //switch (cmb_MeterClass.SelectedItem.ToString())
            //{
            //    case "A":
            //    case "2": b = 480; break;
            //    case "B":
            //    case "1":
            //    case "C":
            //    case "0.5S": b = 600; break;
            //    case "D":
            //    case "0.2S":
            //    case "E": b = 900; break;
            //}
            //A 2
            b = 480;
            txt_jjg596_2012_creepTime.Text = Math.Round(b * 1e6 / (C * m * Un * Imax), 5).ToString();
            //B 1
            b = 600;
            txt_jjg596_2012_creepTime_1.Text = Math.Round(b * 1e6 / (C * m * Un * Imax), 5).ToString();
            //C 0.5S
            b = 600;
            txt_jjg596_2012_creepTime_05S.Text = Math.Round(b * 1e6 / (C * m * Un * Imax), 5).ToString();
            //D 0.2S E
            b = 900;
            txt_jjg596_2012_creepTime_02S.Text = Math.Round(b * 1e6 / (C * m * Un * Imax), 5).ToString();

            //JJG596-202X
            switch (cmb_Transformer.SelectedItem.ToString())
            {
                case "Direct":
                    switch (cmb_MeterClass.SelectedItem.ToString())
                    {
                        case "A":
                        case "2": Iqx = 0.05; break;
                        case "B":
                        case "1": Iqx = 0.04; break;
                        case "C":
                        case "0.5S":
                        case "D":
                        case "0.2S":
                        case "E": Iqx = 0.04; break;
                    }
                    break;
                case "Transformer":
                    switch (cmb_MeterClass.SelectedItem.ToString())
                    {
                        case "A":
                        case "2": Iqx = 0.05; break;
                        case "B":
                        case "1": Iqx = 0.04; break;
                        case "C":
                        case "0.5S":
                        case "D":
                        case "0.2S":
                        case "E": Iqx = 0.02; break;
                    }
                    break;
            }
            txt_jjg596_202X_startxIb.Text = Iqx.ToString();
            double Ist = Iqx * Itr;
            txt_jjg596_202X_startTime.Text = Math.Round(3.6 * 1e6 / (C * m * Unom * Ist), 5).ToString();
            switch (cmb_MeterClass.SelectedItem.ToString())
            {
                case "A":
                case "2": b = 2.5; break;
                case "B":
                case "1": b = 1.5; break;
                case "C":
                case "0.5S": b = 1.0; break;
                case "D":
                case "0.2S": b = 0.4; break;
                case "E": b = 0.2; break;
            }
            txt_jjg596_202X_creepTime.Text = Math.Round(100 * 1e3 / (1.1 * b * C * m * Unom * Imin), 5).ToString();


        }

        private void cmb_PQ_SelectedIndexChanged(object sender, EventArgs e)
        {
            PowerHelper.CalculatePhaseDegree($"M{cmb_TestMode.SelectedItem.ToString()}", cmb_PQ.SelectedItem.ToString(), cmb_FR.SelectedItem.ToString() == "-" ? "Reverse" : "Forward", cmb_HABC.SelectedItem.ToString(), factor, cmb_LC.SelectedItem.ToString(), cmb_clockwise.SelectedItem.ToString().ToLower() == "anticlockwise", out PowerHelper.PhiParam phis);
            txt_PhiU1.Text = phis.PhiUa.ToString();
            txt_PhiU2.Text = phis.PhiUb.ToString();
            txt_PhiU3.Text = phis.PhiUc.ToString();
            txt_PhiI1.Text = phis.PhiIa.ToString();
            txt_PhiI2.Text = phis.PhiIb.ToString();
            txt_PhiI3.Text = phis.PhiIc.ToString();
        }

        private void cmb_clockwise_SelectedIndexChanged(object sender, EventArgs e)
        {
            PowerHelper.CalculatePhaseDegree($"M{cmb_TestMode.SelectedItem.ToString()}", cmb_PQ.SelectedItem.ToString(), cmb_FR.SelectedItem.ToString() == "-" ? "Reverse" : "Forward", cmb_HABC.SelectedItem.ToString(), factor, cmb_LC.SelectedItem.ToString(), cmb_clockwise.SelectedItem.ToString().ToLower() == "anticlockwise", out PowerHelper.PhiParam phis);
            txt_PhiU1.Text = phis.PhiUa.ToString();
            txt_PhiU2.Text = phis.PhiUb.ToString();
            txt_PhiU3.Text = phis.PhiUc.ToString();
            txt_PhiI1.Text = phis.PhiIa.ToString();
            txt_PhiI2.Text = phis.PhiIb.ToString();
            txt_PhiI3.Text = phis.PhiIc.ToString();
        }
    }
}